/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nus.application.ejb.conference.impl;

import com.nus.application.ejb.conference.ConferenceSubmitEjbLocal;
import com.nus.dataaccess.dao.author.Author;
import com.nus.dataaccess.dao.author.dao.AuthorDao;
import com.nus.dataaccess.dao.conference.Conference;
import com.nus.dataaccess.dao.conference.ConferenceAuthor;
import com.nus.dataaccess.dao.conference.ConferenceItem;
import com.nus.dataaccess.dao.conference.dao.ConferenceAuthorDao;
import com.nus.dataaccess.dao.conference.dao.ConferenceDao;
import com.nus.dataaccess.dao.conference.dao.ConferenceItemDao;
import com.nus.dataaccess.dao.util.HibernateUtil;
import com.nus.dataaccess.scalar.FileUploadScalar;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.annotation.security.PermitAll;
import javax.ejb.Stateless;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;

/**
 *
 * @author robin.foe
 */

@Stateless
@PermitAll
public class ConferenceSubmitEjb implements ConferenceSubmitEjbLocal{

    @Override
    public List<Conference> paginate(int first, int pageSize, String sortField, boolean ascending, Map<String, Object> filters) {
        return ConferenceDao.getInstance().paginate(first, pageSize, sortField, ascending, filters);
    }

    @Override
    public int getTotalCount(Map<String, Object> filters) {
        return ConferenceDao.getInstance().getTotalCount(filters);
    }

    @Override
    public ConferenceAuthor getConferenceAuthor(Long conferenceId, Long userId) {
        Conference conference = ConferenceDao.getInstance().getConferenceById(conferenceId);
        for(ConferenceAuthor conferenceAuthor : conference.getConferenceAuthors()){
            for(Author author : conferenceAuthor.getAuthors()){
                if(author.getUserAccountId().equals(userId))
                    return conferenceAuthor;
            }
        }
        return null;
        //return ConferenceAuthorDao.getInstance().get(conferenceAuthorId);
    }
    
    
    @Override
    public void persistConferenceAuthor(ConferenceAuthor conferenceAuthor, List<FileUploadScalar> fileUploadScalars){
        
        try{
            HibernateUtil.beginTransaction();
            Long conferenceAuthorId;
            List<Author> authors = conferenceAuthor.getAuthors();
            if(conferenceAuthor.isPkSet()){
                ConferenceAuthor conferenceAuthorDb = ConferenceAuthorDao.getInstance().get(conferenceAuthor.getId());

                List<Author> deleteList = new ArrayList<Author>();
                for(Author dbAuthor : conferenceAuthorDb.getAuthors() ){
                        if(!authors.contains(dbAuthor))
                            deleteList.add(dbAuthor);
                }

                for(Author deleteAuthor : deleteList){
                    System.err.println("AUTHOR ID "+ deleteAuthor.getId()) ;
                    ConferenceAuthorDao.getInstance().get(deleteAuthor.getId());
                    AuthorDao.getInstance().delete(AuthorDao.getInstance().get(deleteAuthor.getId()));
                }

                for(Author author : authors){
                    if(!author.isPkSet()){
                        author.setConferenceAuthorId(conferenceAuthorDb.getId());
                        AuthorDao.getInstance().saveOrUpdate(author);
                    }
                        
                    else{
                        Author dbAuthor = AuthorDao.getInstance().get(author.getId());
                        dbAuthor.setUserAccountId(author.getUserAccountId());
                        AuthorDao.getInstance().saveOrUpdate(dbAuthor);
                    }
                }
                conferenceAuthorId = conferenceAuthorDb.getId();
            }else{
                ConferenceAuthor insertConferenceAuthor = new ConferenceAuthor();
                insertConferenceAuthor.setConferenceId(conferenceAuthor.getConferenceId());
                ConferenceAuthorDao.getInstance().save(insertConferenceAuthor);
                for(Author author : authors){
                    author.setConferenceAuthorId(insertConferenceAuthor.getId());
                    ConferenceAuthorDao.getInstance().save(author);
                }
                conferenceAuthorId = insertConferenceAuthor.getId();
            }
            
            for(FileUploadScalar fileUploadScalar : fileUploadScalars){
                if(StringUtils.isEmpty(fileUploadScalar.getFileUploadFileName()))
                    continue;
                
                byte[] bytes = FileUtils.readFileToByteArray(fileUploadScalar.getFileUpload());
                ConferenceItem conferenceItem = new ConferenceItem();
                conferenceItem.setFileBinary(bytes);
                conferenceItem.setFileName(fileUploadScalar.getFileUploadFileName());
                conferenceItem.setKeyWord(fileUploadScalar.getKeyWord());
                conferenceItem.setType(fileUploadScalar.getFileUploadContentType());
                conferenceItem.setConferenceAuthorId(conferenceAuthorId);
                ConferenceItemDao.getInstance().save(conferenceItem);
            }
            
            HibernateUtil.commitTransaction();
        }catch(Exception e){
            HibernateUtil.rollbackTransaction();
            e.printStackTrace();
        }
        
        
        
    
    }
    
    @Override
    public void removeConferenceItem(Long conferenceItemId){
        try{
            HibernateUtil.beginTransaction();
            ConferenceItemDao.getInstance().delete(ConferenceItemDao.getInstance().get(conferenceItemId));
            HibernateUtil.commitTransaction();   
        }catch(Exception e){
            HibernateUtil.rollbackTransaction();
            e.printStackTrace();
        }
    }
    
    @Override
    public ConferenceItem getConferenceItem(Long conferenceItemId){
        return ConferenceItemDao.getInstance().get(conferenceItemId);
    }
    
}
